package com.example.rydwlib.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.baselib.fragment.BaseFragment

import com.example.rydwlib.R
import com.example.rydwlib.RydwViewModel
import com.example.rydwlib.adapter.RyPersonAdapter
import com.example.rydwlib.bean.RyPersonData
import kotlinx.android.synthetic.main.fragment_ry_main_person.*
import kotlinx.android.synthetic.main.fragment_rydw_person.*
import java.lang.Exception


/**
 * A simple [Fragment] subclass.
 */
class RyMainPersonFragment : BaseFragment<RydwViewModel>() {
    private val personAdapter: RyPersonAdapter by lazy { RyPersonAdapter(R.layout.ry_person_item) }
    private var type: String = "personlist"
    private  var list:List<RyPersonData> = mutableListOf()
    private  var selectMap: HashMap<String,String> = HashMap()//筛选条件
    override fun providerVMClass(): Class<RydwViewModel>?=RydwViewModel::class.java

    override fun contentLayout(): Int=R.layout.fragment_ry_main_person

    override fun initView() {
        ry_main_person_srl.setOnRefreshListener {
            viewModel.isLoad = false
            viewModel.loadRyPersonData(type)
        }
        personAdapter.emptyView =
            LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
        ry_main_person_rcy.layoutManager =
            LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        ry_main_person_rcy.adapter = personAdapter
        personAdapter.bindToRecyclerView(ry_main_person_rcy)
        personAdapter.setOnItemChildClickListener { adapter, view, position ->
            val viewByPosition = adapter.getViewByPosition(position, R.id.ry_department_ly)
            val img = adapter.getViewByPosition(position, R.id.ry_person_expand_img) as ImageView
            if(viewByPosition?.isVisible!!){
                viewByPosition?.isVisible=false
                img.setImageResource(R.mipmap.expand)
            }else{
                viewByPosition?.isVisible=true
                img.setImageResource(R.mipmap.fold)
            }
        }
        ry_main_person_select.setOnClickListener {
            SelectDialog(list).setCallbackListener(object :SelectDialog.onClickCallback{

                override fun click(map: HashMap<String, String>) {
                    selectMap=map
                    setAdapterData()
                }
            }).show(activity!!.supportFragmentManager,"selectdialog")
        }
        ry_main_person_clear.setOnClickListener {
            if (selectMap.isNotEmpty()){
                selectMap.clear()
                personAdapter.setNewData(list)
                ry_main_person_count.text = "共" + list.size + "人"
            }
        }
    }
    private fun setAdapterData(){
        var tempList= mutableListOf<RyPersonData>()
        for ( person in list){
            if (!selectMap["department"].isNullOrEmpty()&&person.department!=selectMap["department"]){
                continue
            }
            if (!selectMap["post"].isNullOrEmpty()&&person.personPost!=selectMap["post"]){
                continue
            }
            if (!selectMap["region"].isNullOrEmpty()&&person.regionname!=selectMap["region"]){
                continue
            }
            if (!selectMap["station"].isNullOrEmpty()&&person.stationname!=selectMap["station"]){
                continue
            }
            if (!selectMap["namecard"].isNullOrEmpty()&&(person.cardnumber!=selectMap["namecard"]&&person.personname!=selectMap["namecard"])){
                continue
            }
            tempList.add(person)
        }
        personAdapter.setNewData(tempList)
        ry_main_person_count.text = "共" + tempList.size + "人"
    }
    override fun initData() {
        val bundle: Bundle = arguments!!
        when(bundle.getInt("type")){
            1 -> {
                type="personlist"
                ry_main_person_type.text="井下人数"
            }
            2 -> {
                type="speciallist"
                ry_main_person_type.text="井下特种人员"
            }
            3 -> {
                type ="leaderlist"
                ry_main_person_type.text="带班领导"
            }
        }
        viewModel.getPersonData(type).observe(this, Observer {
            list= it
            if (selectMap.isNotEmpty()){
                setAdapterData()
            }else{
                personAdapter.setNewData(it)
                ry_main_person_count.text = "共" + it.size + "人"
            }
            ry_main_person_empty.visibility=View.INVISIBLE
        })
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            ry_main_person_nickname.text=viewModel.getUnitInfo()["unitname"]
        }
    }
    override fun requestError(it: Exception?) {
        ry_main_person_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            ry_main_person_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            ry_main_person_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            ry_main_person_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            ry_main_person_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        }
        ry_main_person_empty.findViewById<Button>(R.id.err_rest_btn)
            .setOnClickListener { viewModel.loadRyPersonData(type) }




    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        ry_main_person_srl.isRefreshing = false
    }
}
